package action;

import java.io.File;
import java.io.FileInputStream;
import java.util.Enumeration;

import model.MaskInfo;
import model.PageBean;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.dispatcher.multipart.MultiPartRequestWrapper;

import util.ExcelUtil;
import util.ResponseUtil;
import util.StringUtil;
import dao.MaskInfoDao;

public class MaskInfoAction extends BaseAction {

	private static final long serialVersionUID = -1614985647573236398L;
	private String page;
	private String rows;
	private String keyword;
	private MaskInfo maskInfo;
	private String delIds;
	private String mid;

	public String getPage() {
		return page;
	}

	public void setPage(String page) {
		this.page = page;
	}

	public String getRows() {
		return rows;
	}

	public void setRows(String rows) {
		this.rows = rows;
	}

	public String getKeyword() {
		return keyword;
	}

	public void setKeyword(String keyword) {
		this.keyword = keyword;
	}

	public MaskInfo getMaskInfo() {
		return maskInfo;
	}

	public void setMaskInfo(MaskInfo maskInfo) {
		this.maskInfo = maskInfo;
	}

	public String getDelIds() {
		return delIds;
	}

	public void setDelIds(String delIds) {
		this.delIds = delIds;
	}

	public String getMid() {
		return mid;
	}

	public void setMid(String mid) {
		this.mid = mid;
	}
	
	MaskInfoDao maskInfoDao = new MaskInfoDao();

	@Override
	public String execute() throws Exception {
		PageBean pageBean = new PageBean(page == null? 0 : Integer.parseInt(page),
				rows == null ? 0 : Integer.parseInt(rows));
		try {
			JSONObject result = new JSONObject();
			JSONArray jsonArray = maskInfoDao
					.findAllMaskInfo(keyword, pageBean);
			int total = maskInfoDao.getMaskInfoCount(keyword);
			result.put("rows", jsonArray);
			result.put("total", total);
			result.put("success", "true");
			ResponseUtil.write(ServletActionContext.getResponse(), result);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public String delete() throws Exception {
		try {
			JSONObject result = new JSONObject();
			int delNums = maskInfoDao.deleteMaskInfo(delIds);
			if (delNums > 0) {
				result.put("success", "true");
				result.put("delNums", delNums);
			} else {
				result.put("errorMsg", "删除失败");
			}
			ResponseUtil.write(ServletActionContext.getResponse(), result);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public String save() throws Exception {
		if (StringUtil.isNotEmpty(mid)) {
			maskInfo.setMid(mid);
		}
		try {
			int saveNums = 0;
			JSONObject result = new JSONObject();
			if (StringUtil.isNotEmpty(maskInfo.getMid())) {
				saveNums = maskInfoDao.updateMaskInfo(maskInfo);
			} else {
				saveNums = maskInfoDao.addMaskInfo(maskInfo);
			}
			if (saveNums > 0) {
				result.put("success", "true");
			} else {
				result.put("success", "false");
				result.put("errorMsg", "保存失败");
			}
			ResponseUtil.write(ServletActionContext.getResponse(), result);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	// public String export() throws Exception{
	// Connection con = null;
	// try {
	// con = dbUtil.getCon();
	// Workbook wb=ExcelUtil.fillExcelDataWithTemplate(goodsDao.exportData(con),
	// "goodsTemp.xls");
	// ResponseUtil.export(ServletActionContext.getResponse(), wb,
	// "����excel.xls");
	// } catch (Exception e) {
	// // TODO Auto-generated catch block
	// e.printStackTrace();
	// }
	// return null;
	// }

//	public String upload() throws Exception {
//		POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(
//				userUploadFile));
//		HSSFWorkbook wb = new HSSFWorkbook(fs);
//		HSSFSheet hssfSheet = wb.getSheetAt(0);
//		if (hssfSheet != null) {
//			for (int rowNum = 1; rowNum <= hssfSheet.getLastRowNum(); rowNum++) {
//				HSSFRow hssfRow = hssfSheet.getRow(rowNum);
//				if (hssfRow == null) {
//					continue;
//				}
//				MaskInfo maskInfo = new MaskInfo();
//				maskInfo.setOwner(ExcelUtil.formatCell(hssfRow.getCell(1)));
//				maskInfo.setTabName(ExcelUtil.formatCell(hssfRow.getCell(2)));
//				maskInfo.setTabColumn(ExcelUtil.formatCell(hssfRow.getCell(3)));
//				maskInfo.setFuncName(ExcelUtil.formatCell(hssfRow.getCell(4)));
//				maskInfo.setFuncParam(ExcelUtil.formatCell(hssfRow.getCell(5)));
//				maskInfo.setFuncComment(ExcelUtil.formatCell(hssfRow.getCell(6)));
//				try {
//					maskInfoDao.addMaskInfo(maskInfo);
//				} catch (Exception e) {
//					e.printStackTrace();
//				}
//			}
//		}
//		JSONObject result = new JSONObject();
//		result.put("success", "true");
//		ResponseUtil.write(ServletActionContext.getResponse(), result);
//		return null;
//	}
	
	public String upload() throws Exception {
		MultiPartRequestWrapper multiWrapper = 
            (MultiPartRequestWrapper) ServletActionContext.getRequest();
        Enumeration<String> fileParameterNames = multiWrapper.getFileParameterNames();
        while (fileParameterNames != null && fileParameterNames.hasMoreElements()) 
        {     
            String inputName = (String) fileParameterNames.nextElement();     
            String[] fileName = multiWrapper.getFileNames(inputName);     
            if (isNonEmpty(fileName)) {     
                File[] files = multiWrapper.getFiles(inputName); 
                for(File file : files) {
                	saveMaskInfo(file);
                }
            }  
        }
		JSONObject result = new JSONObject();
		result.put("success", "true");
		ResponseUtil.write(ServletActionContext.getResponse(), result);
		return null;
	}
	
	private void saveMaskInfo(File file) {
		try {
			POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(file));
			HSSFWorkbook wb = new HSSFWorkbook(fs);
			wb = new HSSFWorkbook(fs);
			HSSFSheet hssfSheet = wb.getSheetAt(0);
			if (hssfSheet != null) {
				for (int rowNum = 1; rowNum <= hssfSheet.getLastRowNum(); rowNum++) {
					HSSFRow hssfRow = hssfSheet.getRow(rowNum);
					if (hssfRow == null) {
						continue;
					}
					MaskInfo maskInfo = new MaskInfo();
					maskInfo.setOwner(ExcelUtil.formatCell(hssfRow.getCell(1)));
					maskInfo.setTabName(ExcelUtil.formatCell(hssfRow.getCell(2)));
					maskInfo.setTabColumn(ExcelUtil.formatCell(hssfRow.getCell(3)));
					maskInfo.setFuncName(ExcelUtil.formatCell(hssfRow.getCell(4)));
					maskInfo.setFuncParam(ExcelUtil.formatCell(hssfRow.getCell(5)));
					maskInfo.setFuncComment(ExcelUtil.formatCell(hssfRow.getCell(6)));
					maskInfoDao.addMaskInfo(maskInfo);
				}
			}
		} catch (Exception e1) {
			e1.printStackTrace();
		}
	}

	private boolean isNonEmpty(Object[] objArray) {     
	    boolean result = false;     
	    for (int index = 0; index < objArray.length && !result; index++) {     
	        if (objArray[index] != null) {     
	            result = true;     
	        }     
	    }     
	    return result;     
	}
}
